import {
  list as wxPageList
} from '../../../../api/wxPage'
const util = require('../../../../utils/util')
import {
  list as navigationList
} from '../../../../api/navigation'
let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object,
    }
  },
  lifetimes: {
    attached() {
      if (this.data.info.tagids != null) {
        this.data.tagIds = this.data.info.tagids.split(',')
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
      }
      this.setData({
        tagIds: this.data.tagIds
      })

      this.loadPage()
      this.loadnavigation()
    }
  },
  /**
   * 组件的初始数据
   */
  data: {

    navigations: [],
    pageList: [],
    tagIds: [],
    haveThumbnial: false
  },

  created() {


  },
  ready: function (e) {

  },

  /**
   * 组件的方法列表
   */

  methods: {
    loadnavigation() {
      if (this.data.tagIds.length > 0) {
        navigationList({
          mchId: app.globalData.mchId,
          tagIds: this.data.tagIds
        }).then(res => {
          res.data.data.forEach(item => {
            item.logo = util.thumbnail(item.logo, 430, 196)
          })
          this.data.navigations = res.data.data
          this.setData({
            navigations: this.data.navigations
          })
        })
      } else {
        navigationList({
          mchId: app.globalData.mchId,
        }).then(res => {
          this.data.navigations = res.data.data
          this.setData({
            navigations: this.data.navigations
          })
        })
      }
    },
    topage(e) {

      wx.navigateTo({
        url: '/pages/index/wxPage/wxPage?id=' + e.currentTarget.dataset.id
      })

    },
    toLink(e) {
      console.log(e)
      if (e.currentTarget.dataset.id.type == 1) {
        wx.navigateTo({
          url: '/pages/goods/category/category?id=' + e.currentTarget.dataset.id.typeId
        })
      }
      if (e.currentTarget.dataset.id.type == 2) {
        wx.navigateTo({
          url: '/pages/index/wxPage/wxPage?id=' + e.currentTarget.dataset.id.typeId
        })
      }
      if (e.currentTarget.dataset.id.type == 3) {
        wx.navigateTo({
          url: '/pages/goods/product/details?id=' + e.currentTarget.dataset.id.typeId,
        })
      }
      if (e.currentTarget.dataset.type.type === 12) {
        /** 
         * 打开另一个小程序
         * appId，path
         **/
        console.log(e.currentTarget.dataset.type)
        const { typeName, url } = e.currentTarget.dataset.type
        if (typeName.indexOf("mp://") == 0 || url.indexOf("mp://") == 0) {
          if ((typeName.indexOf("appId") > 0 || url.indexOf("appId") > 0) && (typeName.indexOf("path") > 0 || url.indexOf("path") > 0)) {
            let appId_index = typeName.indexOf('appId=',)
            let path_index = typeName.indexOf('&path=',)
            let appId = typeName.substring(appId_index + 6, path_index)
            let path = typeName.substring(path_index + 6)
            // console.log(appId, path, typeName.length)
            wx.navigateToMiniProgram({
              appId: appId,
              path: path,
              success: res => {
          console.log(res)
              },
              fail: err => {
          console.log(err)
              },
            })
          }
        }
            }
    },
    loadPage() {
      var _this = this;
      wxPageList({
        mchId: app.globalData.mchId,
        tagIds: this.data.tagIds,
      }).then(res => {
        _this.data.pageList = res.data.data
        let haveThumbnial = this.data.haveThumbnial
        if (_this.data.pageList.length > 0) {
          if (!util.isNull(_this.data.pageList[0].thumbnial)) {
            _this.data.pageList[0].thumbnial = util.thumbnail(_this.data.pageList[0].thumbnial, 710, 446)
            haveThumbnial = true
          }
        }
        _this.setData({
          pageList: _this.data.pageList,
          haveThumbnial: haveThumbnial
        })
      })
    }
  }
})